// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/dwio/common/selective_floating_point_column_reader.h>

namespace kumo::pollux::parquet {
    template<typename TData, typename TRequested>
    class FloatingPointColumnReader
            : public dwio::common::
            SelectiveFloatingPointColumnReader<TData, TRequested> {
    public:
        using ValueType = TRequested;

        using base =
        dwio::common::SelectiveFloatingPointColumnReader<TData, TRequested>;

        FloatingPointColumnReader(
            const TypePtr &requestedType,
            std::shared_ptr<const dwio::common::TypeWithId> fileType,
            ParquetParams &params,
            common::ScanSpec &scanSpec);

        void seekToRowGroup(int64_t index) override {
            base::seekToRowGroup(index);
            this->scanState().clear();
            this->readOffset_ = 0;
            this->formatData_->template as<ParquetData>().seekToRowGroup(index);
        }

        uint64_t skip(uint64_t numValues) override;

        void read(int64_t offset, const RowSet &rows, const uint64_t *incomingNulls)
        override {
            using T = FloatingPointColumnReader<TData, TRequested>;
            this->template readCommon<T, true>(offset, rows, incomingNulls);
            this->readOffset_ += rows.back() + 1;
        }

        template<typename TVisitor>
        void readWithVisitor(const RowSet &rows, TVisitor visitor);
    };

    template<typename TData, typename TRequested>
    FloatingPointColumnReader<TData, TRequested>::FloatingPointColumnReader(
        const TypePtr &requestedType,
        std::shared_ptr<const dwio::common::TypeWithId> fileType,
        ParquetParams &params,
        common::ScanSpec &scanSpec)
        : dwio::common::SelectiveFloatingPointColumnReader<TData, TRequested>(
            requestedType,
            std::move(fileType),
            params,
            scanSpec) {
    }

    template<typename TData, typename TRequested>
    uint64_t FloatingPointColumnReader<TData, TRequested>::skip(
        uint64_t numValues) {
        return this->formatData_->skip(numValues);
    }

    template<typename TData, typename TRequested>
    template<typename TVisitor>
    void FloatingPointColumnReader<TData, TRequested>::readWithVisitor(
        const RowSet &rows,
        TVisitor visitor) {
        this->formatData_->template as<ParquetData>().readWithVisitor(visitor);
    }
} // namespace kumo::pollux::parquet
